package com.marktotop.controller;


import com.marktotop.entity.Department;
import com.marktotop.response.ResultBean;
import com.marktotop.response.ResultCode;
import com.marktotop.service.IDepartmentService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author marktotop
 * @since 2021-05-23
 */
@RestController
@Api(tags = "部门管理接口")
@RequestMapping("/system/basic/department")
public class DepartmentController {
    @Autowired
    private IDepartmentService departmentService;
    @ApiOperation("查询所有部门")
    @GetMapping("/")
    public ResultBean getAllDepartment(){
        List<Department> allDepartment = departmentService.getAllDepartment();
        if (null == allDepartment){
            return ResultBean.result(ResultCode.NON_RESULT);
        }
        return ResultBean.ok().data("department",allDepartment);
    }

    /**
     * 添加部门
     *
     * @param department
     * @return
     */
    @ApiOperation("添加部门")
    @PostMapping("/add")
    public ResultBean addDept(@RequestBody Department department){
       department.setEnabled(true);
       departmentService.addDept(department);
       if (1==department.getResult()){
           return ResultBean.ok().message("添加部门成功").data("addMessage",department);
       }
        return ResultBean.error().message("添加部门失败");
    }


    /**
     * 删除部门
     *      1 代表  删除成功
     *      -1代表  该部门下有员工，无法删除
     *      -2代表  该部门存在子部门，无法删除
     *
     * @param id
     * @return
     */
    @ApiOperation("删除部门")
    @DeleteMapping("/del/{id}")
    public ResultBean delDept(@PathVariable("id") Integer id){
        Department department = new Department();
        department.setId(id);
        departmentService.delDept(department);
        if (1==department.getResult()){
            return ResultBean.ok().message("部门删除成功").data("delMessage",department);
        }
        else if(-2 == department.getResult()){
            return ResultBean.error().message("该部门存在子部门，请删除后重试！");
        }
        else if(-1 == department.getResult()){
            return ResultBean.error().message("该部门下有员工，无法删除！");
        }else {
            return ResultBean.error().message("删除部门操作失败，请重试...");
        }
    }
    @ApiOperation("修改部门")
    @DeleteMapping("/modify/{id}")
    public ResultBean modifyDept(@PathVariable("id") Integer id){
        return null;
    }
}
